<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import * as admin from '@/apis/admin'
import { useUserInfo } from '@/stores/userInfo'
const userInfo = useUserInfo()
const route = useRoute()

const loding = ref(true)
let Router = useRouter()
const form = reactive({
  name: '',
  password: ''
})

if (route?.query?.test != undefined) {
  form.name = 'test'
  form.password = 'test123456'
}

const login = () => {
  loding.value = true
  admin.login(form.name, form.password).then(res => {
    if (res.data.code != 200) {
      ElMessage(res.data.msg)
      loding.value = false
      return
    }
    cookieLogin()
  })
}

const cookieLogin = () => {
  userInfo.getUserInfo(true).then(res => {
    if (userInfo.username) {
      Router.replace({ path: `/home/index` })
    }
    loding.value = false
  })
}
cookieLogin()
</script>

<template>
  <el-main v-loading="loding" v-show="loding" style="height: 100vh; width: 100vw; z-index: 999999"></el-main>
  <el-dialog width="400px" :show-close="false" :close-on-click-modal="false" :close-on-press-modal="false" model-value="true" title="后台登录">
    <el-form :model="form">
      <el-form-item label="账号">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" autocomplete="off" type="password" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="login"> 登录 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped></style>
